package com.gocoolmall.order.listener;

import com.alibaba.fastjson.JSON;
import com.gocoolmall.common.to.mq.SeckillOrderTo;
import com.gocoolmall.order.entity.OrderEntity;
import com.gocoolmall.order.service.OrderService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
@Slf4j
@RabbitListener(queues = "order.seckill.order.queue")
@Component
public class OrderSeckillListener {
    @Autowired
    OrderService orderService;
    @RabbitHandler
    public void listener(SeckillOrderTo entity, Channel channel, Message message) throws IOException {
        try {
            log.info("准备创建秒杀单的详细信息...{}", JSON.toJSONString(entity));
            orderService.createSeckillOrder(entity);
            //TODO手动调用支付宝收单

            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);//消息接收确认
        } catch (Exception e) {
            e.printStackTrace();
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);//拒绝接收一个消息
        }
    }
}
